﻿Imports SistemaINCA.Security
Namespace Models
    Public Interface IResponsable
        <Authorize(Roles:="ERA")> _
        Function GetResponsablesDeAlumno(NIE As String) As List(Of RESPONSABLE_BYALUMNO_DATATYPE)

        <Authorize(Roles:="ERA")> _
        Function UpdateResponsable(IDResponsable As Integer, DUI As String, Profesion As String, LTrabajo As String, Dir As String, Telefono As String, CorreoE As String) As Boolean

        <Authorize(Roles:="ERA")> _
        Function DeleteResponsable(IDResponsable As Integer, NIE As String) As Boolean
    End Interface

    Partial Public Class SchoolClickDB_Repositorio
        Implements IResponsable

        Public ReadOnly Property IResponsables() As IResponsable
            Get
                Return DirectCast(Me, IResponsable)
            End Get
        End Property

        Private Function GetResponsablesDeAlumno(NIE As String) As List(Of RESPONSABLE_BYALUMNO_DATATYPE) Implements IResponsable.GetResponsablesDeAlumno
            Try
                Using cnn As New SchoolClickEntities
                    Return cnn.GET_RESPONSABLE_BYALUMNO(NIE).ToList
                End Using
            Catch ex As Exception
                Throw New ApplicationException(String.Format("No se pudo obtener los responsables del alumno con No. de NIE {1}. {0}", ex.Message, NIE), ex)
            End Try
        End Function

        Private Function UpdateResponsable(IDResponsable As Integer, DUI As String, Profesion As String, LTrabajo As String, Dir As String, Telefono As String, CorreoE As String) As Boolean Implements IResponsable.UpdateResponsable
            Try
                Using cnn As New SchoolClickEntities
                    Dim b = cnn.UPT_DATOS_RESPONSABLE(IDResponsable, DUI, Profesion, Telefono, LTrabajo, Dir, CorreoE, EncryptationSchoolClick.ToStringInseguro(EncryptationSchoolClick.DesencriptarData(ConfigurationManager.AppSettings("Rol_ERA")))).SingleOrDefault
                    If b Then Return True Else Return False
                End Using
            Catch ex As EntityCommandExecutionException
                Throw New ApplicationException(String.Format("No se pudieron actualizar los datos del responsables No. {1}. {0}", ex.InnerException.Message, IDResponsable), ex)
            Catch ex As Exception
                Throw New ApplicationException(String.Format("No se pudieron actualizar los datos del responsables No. {1}. {0}", ex.Message, IDResponsable), ex)
            End Try
        End Function

        Private Function DeleteResponsable(IDResponsable As Integer, NIE As String) As Boolean Implements IResponsable.DeleteResponsable
            Try
                Using cnn As New SchoolClickEntities
                    Dim b = cnn.DEL_RESPONSABLE_ALUMNO(IDResponsable, NIE, EncryptationSchoolClick.ToStringInseguro(EncryptationSchoolClick.DesencriptarData(ConfigurationManager.AppSettings("Rol_ERA")))).SingleOrDefault
                    If b Then Return True Else Return False
                End Using
            Catch ex As EntityCommandExecutionException
                Throw New ApplicationException(String.Format("No se pudo eliminar el responsables asociado al alumno con No. de NIE {1}. {0}", ex.InnerException.Message, NIE), ex)
            Catch ex As Exception
                Throw New ApplicationException(String.Format("Ocurrio un error inesperado al intentar eliminar el responsable. {0}", ex.Message, IDResponsable), ex)
            End Try
        End Function
    End Class
End Namespace
